package com.ruoyi.miaomiao.controller.anonymous;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.miaomiao.domain.Order;
import com.ruoyi.miaomiao.service.IOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 服务于用户的订单表crud的Controller
 */
@Api(tags = "用户订单管理") //  tags：你可以当作是这个组的名字。
@RestController
@RequestMapping("/order")
@ApiResponses(value = {@ApiResponse(code = 0, message = "订单对象信息", response = Order.class)})
public class AnOrderController extends BaseController
{
    //TODO: 以下crud都仍需要身份验证方法

    @Autowired
    private IOrderService orderService;

    /**
     * 按用户id查询订单列表
     */
    @ApiOperation(value = "按用户id查询订单列表",notes = "通过用户id返回用户的订单列表")
    @GetMapping("/{userId}")
    public TableDataInfo list(@PathVariable("userId") Long userId)
    {
        startPage();
        List<Order> list = orderService.selectOrderListByUserIds(userId);
        return getDataTable(list);
    }

    /**
     * 获取订单详细信息
     */
    @ApiOperation(value = "获取用户订单详细信息",notes = "通过指定用户id及用户所属订单id获取该订单所有信息")
    @GetMapping(value = "/{userId}/{orderId}")
    public AjaxResult getInfo(@PathVariable("userId") Long userId, @PathVariable("orderId") Long orderId)
    {
        return AjaxResult.success(orderService.selectOrderByUserAndOrderId(userId, orderId));
    }

    /**
     * 新增订单
     */
    @ApiOperation(value = "新增用户订单",notes = "通过上传Order类型数据新增一条订单，其中userId和goodsId必填，勿填orderId")
    @PostMapping
    public AjaxResult add(@RequestBody Order order)
    {
        return toAjax(orderService.insertOrder(order));
    }

    /**
     * 修改订单
     */
    @PutMapping
    @ApiOperation(value = "修改用户订单",notes = "通过上传Order类型数据，根据指定orderId和userId修改该订单信息")
    public AjaxResult edit(@RequestBody Order order)
    {
        return toAjax(orderService.updateOrder(order));
    }

    /**
     * 删除订单管理
     */
    @ApiOperation(value = "删除用户订单",notes = "通过指定userId和orderIds（可为数组）删除指定订单")
	@DeleteMapping("/{userId}/{orderIds}")
    public AjaxResult remove(@PathVariable("userId") Long userId, @PathVariable Long[] orderIds)
    {
        return toAjax(orderService.deleteOrderByUserAndOrderIds(userId, orderIds));
    }
}
